﻿Imports System.Data.SqlClient

Module PublicFunctionClass
    ''' <summary>
    ''' 根据ID获取CONFIG表中的参数值（CSZ）
    ''' </summary>
    ''' <param name="ID">参数ID</param>
    ''' <returns>返回参数值（CSZ），如果未找到返回空字符串</returns>
    Public Function getConfig(ID As Integer) As String
        ' 定义返回值
        Dim result As String = ""

        ' SQL 查询语句
        Dim query As String = "SELECT CSZ FROM CONFIG WHERE ID = @ID"

        Try
            ' 使用 Using 块确保资源自动释放
            Using conn As New SqlConnection(DbConnectionHelper.GetConnectString)
                conn.Open()

                ' 创建命令对象
                Using cmd As New SqlCommand(query, conn)
                    ' 添加参数
                    cmd.Parameters.AddWithValue("@ID", ID)

                    ' 执行查询
                    Dim obj = cmd.ExecuteScalar()

                    ' 判断查询结果
                    If obj IsNot Nothing AndAlso Not IsDBNull(obj) Then
                        result = obj.ToString()
                    End If
                End Using
            End Using

        Catch ex As Exception
            ' 捕获错误（你可以选择记录日志）
            MsgBox("获取配置参数出错：" & ex.Message, MsgBoxStyle.Critical, "错误")
        End Try

        Return result
    End Function
End Module
